package com.ufgov.ip.service;

import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Clock;

import com.ufgov.ip.dao.IuapHirerDao;
import com.ufgov.ip.entity.IpHirer;
import com.ufgov.ip.sysmanager.dao.ICompanyDao;
import com.ufgov.ip.sysmanager.entity.IpCompany;
import com.ufgov.ip.util.UUIDTools;

/**
 * 用户管理服务层 书写readonly是说明没有特殊标识服务层只有只读事物
 *
 */
@Component
public class HirerRegisterService {

	public static final String HASH_ALGORITHM = "SHA-1";
	public static final int HASH_INTERATIONS = 1024;
	private static final int SALT_SIZE = 8;

	private static Logger logger = LoggerFactory
			.getLogger(HirerRegisterService.class);

	@Autowired
	private IuapHirerDao hirerDao;

	/*@Autowired
	private IuapCompanyDao coDao;*/

	@Autowired
	private ICompanyDao coDao;
	

	public void setCoDao(ICompanyDao coDao) {
		this.coDao = coDao;
	}
	

	private Clock clock = Clock.DEFAULT;

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public boolean registerHirer(IpHirer hirer,IpCompany ipCompany) throws Exception {
		try {
			 if (hirer.getHirerId()==null || hirer.getHirerId()=="") {
				hirer.setHirerId(UUIDTools.uuidRandom());
			}
			  
			  hirerDao.save(hirer);			 
			 //begin_iuap_zhangbch_调试_20160313
			 IpHirer findByHirerName = hirerDao.findByHirerName(hirer.getHirerName());
			 ipCompany.setHirerId((findByHirerName.getHirerId()));
			 ipCompany.setLevelNum(1);
			 coDao.save(ipCompany);
			 //end_iuap_zhangbch_调试_20160313		
		} catch (Exception e) {
			logger.error("注册用户失败!"+e);
			throw new Exception();
		}
		
		return true;
	}

	public void setHirerDao(IuapHirerDao hirerDao) {
		this.hirerDao = hirerDao;
	}

	/*public void setCoDao(IuapCompanyDao coDao) {
		this.coDao = coDao;
	}*/

	/*
	 * private void entryptPassword(IpHirer hirer) {
	 * 
	 * String password=Encodes.encodeHex(hirer.getPassword().getBytes());
	 * hirer.setPassword(password); }
	 */

}
